package com.elitedatai.mchtest.mission.controller;

import com.dji.sdk.common.HttpResultResponse;
import com.elitedatai.mchtest.common.model.CustomClaim;
import com.elitedatai.mchtest.mission.model.dto.FlightPlanDTO;
import com.elitedatai.mchtest.mission.service.IFlightPlanService;
import com.elitedatai.mchtest.wayline.model.param.CreateJobParam;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import static com.elitedatai.mchtest.component.AuthInterceptor.TOKEN_CLAIM;

@RestController
@Slf4j
@RequestMapping("/api/v1/flight-plans")
public class FlightPlanController {

    @Autowired
    private IFlightPlanService flightPlanService;


    @PostMapping("/create")
    public HttpResultResponse<CreateJobParam> createFlightPlan(@Valid @RequestBody FlightPlanDTO flightPlanDTO) {
        try {
            CreateJobParam waylineJobParam = flightPlanService.createFlightPlan(flightPlanDTO);
            return HttpResultResponse.success(waylineJobParam);
        } catch (Exception e) {
            log.error("创建飞行计划失败", e);
            return HttpResultResponse.error("创建飞行计划失败: " + e.getMessage());
        }
    }

    @PostMapping("/{workspace_id}/plan-excute-job")
    public HttpResultResponse createJob(HttpServletRequest request, @Valid @RequestBody CreateJobParam param,
                                        @PathVariable(name = "workspace_id") String workspaceId) throws SQLException {
        CustomClaim customClaim = (CustomClaim)request.getAttribute(TOKEN_CLAIM);
        customClaim.setWorkspaceId(workspaceId);

        return flightPlanService.publishFlightTask(param, customClaim);
    }

    /**
     * 分页查询所有飞行计划
     * @param page 页码，从0开始
     * @param size 每页大小
     * @return 飞行计划分页数据
     */
    @GetMapping("/list")
    public HttpResultResponse<Page<FlightPlanDTO>> getAllFlightPlans(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<FlightPlanDTO> flightPlans = flightPlanService.getAllFlightPlans(pageable);
            return HttpResultResponse.success(flightPlans);
        } catch (Exception e) {
            log.error("查询飞行计划失败", e);
            return HttpResultResponse.error("查询飞行计划失败: " + e.getMessage());
        }
    }

    /**
     * 根据状态分页查询飞行计划
     * @param status 状态
     * @param page 页码，从0开始
     * @param size 每页大小
     * @return 飞行计划分页数据
     */
    @GetMapping("/list/by-status")
    public HttpResultResponse<Page<FlightPlanDTO>> getFlightPlansByStatus(
            @RequestParam Integer status,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<FlightPlanDTO> flightPlans = flightPlanService.getFlightPlansByStatus(status, pageable);
            return HttpResultResponse.success(flightPlans);
        } catch (Exception e) {
            log.error("根据状态查询飞行计划失败", e);
            return HttpResultResponse.error("根据状态查询飞行计划失败: " + e.getMessage());
        }
    }

    /**
     * 根据机场序列号分页查询飞行计划
     * @param dockSn 机场序列号
     * @param page 页码，从0开始
     * @param size 每页大小
     * @return 飞行计划分页数据
     */
    @GetMapping("/list/by-dock")
    public HttpResultResponse<Page<FlightPlanDTO>> getFlightPlansByDockSn(
            @RequestParam String dockSn,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<FlightPlanDTO> flightPlans = flightPlanService.getFlightPlansByDockSn(dockSn, pageable);
            return HttpResultResponse.success(flightPlans);
        } catch (Exception e) {
            log.error("根据机场序列号查询飞行计划失败", e);
            return HttpResultResponse.error("根据机场序列号查询飞行计划失败: " + e.getMessage());
        }
    }

    /**
     * 根据飞手ID分页查询飞行计划
     * @param flyerId 飞手ID
     * @param page 页码，从0开始
     * @param size 每页大小
     * @return 飞行计划分页数据
     */
    @GetMapping("/list/by-flyer")
    public HttpResultResponse<Page<FlightPlanDTO>> getFlightPlansByFlyerId(
            @RequestParam String flyerId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<FlightPlanDTO> flightPlans = flightPlanService.getFlightPlansByFlyerId(flyerId, pageable);
            return HttpResultResponse.success(flightPlans);
        } catch (Exception e) {
            log.error("根据飞手ID查询飞行计划失败", e);
            return HttpResultResponse.error("根据飞手ID查询飞行计划失败: " + e.getMessage());
        }
    }


}
